package DP;

import java.util.Scanner;

/**
 * @author dx
 * @version 1.0
 * @date 2022/5/16 19:44
 * @description: 剑指offer 47 礼物的最大价值
 */
public class Maxvalue {

    public static void main(String[] args) {
        Scanner sr = new Scanner(System.in);
        int m = sr.nextInt();
        int n = sr.nextInt();
        int [][]arr = new int[m][n];
        sr.nextLine();//用于跳过行列后的回车符
        for(int i = 0;i < m;i++){
            for(int j = 0;j < n;j++){
                arr[i][j] = sr.nextInt();
            }
        }
        int ans = new Maxvalue_Solution().maxValue(arr);
        System.out.println(ans);
    }
}


class Maxvalue_Solution{


    public int maxValue(int[][] grid) {

        int m = grid.length;
        int n = grid[0].length;
        int [][]dp = new int[m][n];

        dp[0][0] = grid[0][0];

        for(int i = 0;i<m;i++){
            for(int j = 0;j<n;j++){
                if(i == 0 && j == 0) continue;
                else if(i == 0)  dp[i][j] = dp[i][j-1]+grid[i][j];
                else  if(j == 0)  dp[i][j] = dp[i-1][j]+grid[i][j];
                else{
                    dp[i][j] = Math.max(dp[i-1][j],dp[i][j-1])+grid[i][j];
                }

            }
        }
        return dp[m-1][n-1];
    }
}